5201. Stacks of Bricks

 

You are given a sequence of n (n < 100) integers. Each number denotes the height of a stack of bricks. If we put the stacks in a line as in the illustration below, we would see stacks of uneven heights. Suppose a “move” is made by picking up one brick from one stack and putting it on another, compute the minimum number of moves to rearrange the bricks such that all stacks have the same height.

 

Input. The first line is the integer n, followed by n lines of integers denoting the height of the n stacks. The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.

 

Output. Print one integer denoting the minimum number of moves.

 

Sample input

Sample output

6

5

2

4

1

7

5

5

 

 

РЕШЕНИЕ

линейный массив

 

Анализ алгоритма

 

Реализация алгоритма

 

#include <stdio.h>

 

int summa, i, n, res;

int m[110];

 

int main(void)

{

  scanf("%d",&n);

 

  summa = res = 0;

  for(i = 0; i < n; i++)

  {

    scanf("%d",&m[i]);

    summa += m[i];

  }

 

  summa = summa / n;

  for(i = 0; i < n; i++)

    if (m[i] > summa) res += m[i] - summa;

 

  printf("%d\n",res);

 

  return 0;

}